﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

namespace DNA.Mvc
{
    [Serializable, Table("dna_WebPages")]
    public class WebPage
    {
        public WebPage()
        {
            this.Created = DateTime.UtcNow;
            this.LastModified = DateTime.UtcNow;
            //this.AllowAnonymous = true;
        }

        public WebPage(Web web, string path, string title,string owner, int parentID = 0, bool allowAnonymous = true, 
            bool isShared = false,bool isStatic=false,bool showInMenu=false,string target="_self")
            : this()
        {
            this.Web = web;
            this.Path = path;
            this.Title = title;
            this.Owner = owner;
            this.ParentID = parentID;
            this.AllowAnonymous = allowAnonymous;
            this.IsShared = IsShared;
            this.IsStatic = IsStatic;
            this.ShowInMenu = showInMenu;
            this.Target = target;
        }

        [Key]
        [Column("ID", Order = 1)]
        public int ID { get; set; }

        public int WebID { get; set; }

        [MaxLength(255),Required]
        public string Title { get; set; }

        [Column(TypeName="ntext")]
        public string ViewData { get; set; }

        [NotMapped,XmlIgnore]
        public string[] IgnoreRouteKeys 
        {
            get 
            {
                var keys = new string[] { };
                if (!string.IsNullOrEmpty(ViewData))
                    return ViewData.Split(new char[] { ',' });
                return keys;
            }
        }

        [MaxLength(255)]
        public string ViewName { get; set; }

        [Required]
        public bool AllowAnonymous { get; set; }

        [Required]
        public DateTime Created { get; set; }

        [Column(TypeName = "ntext")]
        public string Description { get; set; }

        [MaxLength(1024)]
        public string IconUrl { get; set; }

        [MaxLength(1024)]
        public string ImageUrl { get; set; }

        [Required]
        public bool IsShared { get; set; }

        [Required]
        public bool IsStatic { get; set; }

        ///// <summary>
        ///// New in V2.1.3
        ///// </summary>
        //[Required]
        //public bool IsMobile { get; set; }

        [Column(TypeName = "ntext")]
        public string Keywords { get; set; }

        [Required]
        public DateTime LastModified { get; set; }

        [MaxLength(1024)]
        public string LinkUrl { get; set; }

        [Required,MaxLength(255)]
        public string Owner { get; set; }

        public int ParentID { get; set; }

        [Required,MaxLength(1024)]
        public string Path { get; set; }

        [Required]
        public int Pos { get; set; }

        [Required]
        public bool ShowInMenu { get; set; }

        [Required,MaxLength(50)]
        public string Target { get; set; }

        [XmlIgnore, ScriptIgnore]
        [ForeignKey("WebID")]
        public virtual Web Web { get; set; }

        [XmlIgnore, ScriptIgnore]
        public virtual ICollection<WebPageRole> Roles { get; set; }

        [ScriptIgnore]
        public virtual ICollection<WidgetInstance> Widgets { get; set; }
    }
}
